package com.ruoyi.map.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.map.domain.RegionInfo;
import com.ruoyi.map.domain.UnitInfo;
import com.ruoyi.map.mapper.RegionInfoMapper;
import com.ruoyi.map.mapper.UnitInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.map.mapper.BuildingInfoMapper;
import com.ruoyi.map.domain.BuildingInfo;
import com.ruoyi.map.service.IBuildingInfoService;

import javax.annotation.Resource;

/**
 * 保障房建筑楼栋信息数据Service业务层处理
 *
 * @author dihang
 * @date 2023-07-26
 */
@Service
public class BuildingInfoServiceImpl extends ServiceImpl<BuildingInfoMapper, BuildingInfo> implements IBuildingInfoService
{
    @Resource
    BuildingInfoMapper buildingInfoMapper;
    @Resource
    RegionInfoMapper regionInfoMapper;
    @Resource
    UnitInfoMapper unitInfoMapper;


    /**
     * 查询保障房建筑楼栋信息数据列表
     *
     *
     * @param
     * @return 列表查询保障房建筑楼栋信息数据
     */
    @Override
    public List<BuildingInfo> selectBuildingInfoList()
    {
        List<BuildingInfo> buildingInfos = buildingInfoMapper.selectList(null);
        if (buildingInfos==null){
            throw new BaseException("保障房建筑楼栋信息数据为空！");
        }
        for (BuildingInfo buildingInfo : buildingInfos) {
            Long id = buildingInfo.getId();
            //查询出该楼栋所有的单元数据
            List<UnitInfo> unitInfos = unitInfoMapper.selectList(Wrappers.<UnitInfo>lambdaQuery().eq(UnitInfo::getBuildingId, id));
            int size = unitInfos.size();
            //计算楼栋包含的单元数量
            buildingInfo.setIncludeUnitNum(size);
            //将该楼栋所包含的所有单元整理成一个集合
            List<String >nameList=unitInfos.stream().map(UnitInfo::getName).collect(Collectors.toList());
            //进行赋值
            buildingInfo.setIncludeUnitName(nameList);
        }
        return buildingInfos;
    }

    /**
     * 新增保障房建筑楼栋信息数据列表
     * @param buildingInfo
     */
    @Override
    public void addNew(BuildingInfo buildingInfo) {
        if (buildingInfo.getId()==null){
            throw new BaseException("主键id不能为空！");
        }
        LambdaQueryWrapper<BuildingInfo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BuildingInfo::getName,buildingInfo.getName());
        queryWrapper.eq(BuildingInfo::getDeleted,"0");////deleted为是否删除 0 否 1 是
        Long selectCount = buildingInfoMapper.selectCount(queryWrapper);
        if (selectCount>0){
            throw new BaseException("该楼栋已存在，不能重复录入！");
        }
        //传入参数时，根据填入的所属区域来确定区域的主键ID
        String affiliationRegion = buildingInfo.getAffiliationRegion();
        RegionInfo regionInfo = regionInfoMapper.selectOne
                (Wrappers.<RegionInfo>lambdaQuery().eq(RegionInfo::getName, affiliationRegion));
        if (regionInfo==null){
            throw new BaseException("数据库中没有您填写的所属区域！");
        }
        Long id = regionInfo.getId();
        buildingInfo.setRegionId(id);
        this.save(buildingInfo);

    }

    /**
     * 分页-条件查询保障房建筑楼栋信息数据列表
     * @param buildingInfo
     * @return
     */
    @Override
    public IPage<BuildingInfo> buildingSearch(BuildingInfo buildingInfo) {
        //按条件分页查询
        IPage<BuildingInfo>page=this.page(new Page<>(buildingInfo.getPageNum(),
                buildingInfo.getPageSize()),oneSelectWhere(buildingInfo));
        //转化为List集合
        List<BuildingInfo> buildingInfoList = page.getRecords();
        List<BuildingInfo>list=new ArrayList<>();
        for (BuildingInfo info : buildingInfoList) {
            Long id = info.getId();
            //查询出该楼栋所有的单元数据
            List<UnitInfo> unitInfos = unitInfoMapper.selectList(Wrappers.<UnitInfo>lambdaQuery().eq(UnitInfo::getBuildingId, id));
            int size = unitInfos.size();
            //计算楼栋包含的单元数量
            info.setIncludeUnitNum(size);
            //将该楼栋所包含的所有单元整理成一个集合
            List<String >nameList=unitInfos.stream().map(UnitInfo::getName).collect(Collectors.toList());
            //进行赋值
            info.setIncludeUnitName(nameList);
            list.add(info);
        }

        IPage<BuildingInfo>iPage=new Page<>();
        iPage.setRecords(list);
        return iPage;
    }

    /**
     * 条件查询 封装QueryWrapper
     *
     * @param buildingInfo
     */
    private LambdaQueryWrapper<BuildingInfo> oneSelectWhere(BuildingInfo buildingInfo) {
        LambdaQueryWrapper<BuildingInfo>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (buildingInfo==null){
            return lambdaQueryWrapper;
        }
        lambdaQueryWrapper.like(StrUtil.isNotBlank(buildingInfo.getName()),
                BuildingInfo::getName,buildingInfo.getName())
                .eq(BuildingInfo::getDeleted,"0");
        lambdaQueryWrapper.orderByDesc(BuildingInfo::getCreateTime);
        return lambdaQueryWrapper;
    }

    /**
     * 获取保障房建筑楼栋信息数据详细信息
     * @param id
     * @return
     */
    @Override
    public BuildingInfo selectById(Long id) {
        BuildingInfo buildingInfo = buildingInfoMapper.selectById(id);
        Long id1 = buildingInfo.getId();
        List<UnitInfo> unitInfos = unitInfoMapper.selectList(Wrappers.<UnitInfo>lambdaQuery().eq(UnitInfo::getBuildingId, id1));
        int size = unitInfos.size();
        //计算楼栋包含的单元数量
        buildingInfo.setIncludeUnitNum(size);
        //将该楼栋所包含的所有单元整理成一个集合
        List<String >nameList=unitInfos.stream().map(UnitInfo::getName).collect(Collectors.toList());
        //进行赋值
        buildingInfo.setIncludeUnitName(nameList);
        return buildingInfo;
    }

}
